package com.example.demoapp.mapper;

import com.example.demoapp.entity.Employee;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface EmployeeMapper {
    //查询所有
    List<Employee> selecAll();

    //根据id查询
    Employee selecByid(Integer id);
    //新建用户
    @Insert("INSERT INTO employee (username,password,role,name, sex, no,age,description,dep_id) VALUES (#{username},#{password},#{role},#{name}, #{sex}, #{no},#{age}, #{description}, #{dep_id})")
    int inSert(Employee employee);

    // 根据ID删除用户
    @Delete("DELETE FROM employee WHERE id = #{id}")
    int del(Integer id);

    @Update({
        "<script>",
        "UPDATE employee",
        "<set>",
        "   <if test='username != null'>username = #{username},</if>",
        "   <if test='password != null'>password = #{password},</if>",
        "   <if test='role != null'>role = #{role},</if>",
        "   <if test='name != null'>name = #{name},</if>",
        "   <if test='sex != null'>sex = #{sex},</if>",
        "   <if test='no != null'>no = #{no},</if>",
        "   <if test='description != null'>description = #{description},</if>",
        "   <if test='dep_id != null'>dep_id = #{dep_id},</if>",
        "</set>",
        "WHERE id = #{id}",
        "</script>"
    })
    Integer updata(Employee employee);

    //根据用户名查询账号是否存在
    @Select("select * from `employee` where username = #{username}")
    Employee slectByusername(String username);

    //将上传的文件路径数据插入
    @Update("UPDATE `employee` SET filepath = #{downloadPath} WHERE id = #{id}")
    void saveFilepath(String downloadPath,Integer id);
}
